package com.helper.debugger.utils;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class IPAndCountry {
    public interface Callback {
        void onSuccess(String ip, String countryCode);
    }

    public interface StringCallback {
        void onSuccess(String content);
    }

    public static void getIPAndCountryCode(Callback callback) {
        final String[] url = {"https://ipinfo.io/json"};
        HttpHelper.getAsync(url[0], (success, response) -> {
            if (success) {
                try {
                    JSONObject data = new JSONObject(response);
                    callback.onSuccess(data.getString("ip"), data.getString("country"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;
            }
            error(url[0] + " 失败，继续下一个");
            url[0] = "https://api.myip.com";
            HttpHelper.getAsync(url[0], (success2, response2) -> {
                if (success2) {
                    try {
                        JSONObject data = new JSONObject(response2);
                        callback.onSuccess(data.getString("ip"), data.getString("cc"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    return;
                }
                error(url[0] + " 失败，继续下一个");
                getIP(ip -> {
                    if (ip != null && !ip.isEmpty()) {
                        getCountryCodeByIP(ip, countryCode -> {
                            if (countryCode != null && !countryCode.isEmpty()) {
                                callback.onSuccess(ip, countryCode);
                            }
                        });
                    }
                });
            });
        });
    }

    public static void getIP(StringCallback callback) {
        final String[] url = {"https://api.ipify.org?format=json"};
        HttpHelper.getAsync(url[0], (success, response) -> {
            if (success) {
                try {
                    JSONObject data = new JSONObject(response);
                    callback.onSuccess(data.getString("ip"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;
            }
            error(url[0] + " 失败，继续下一个");
            url[0] = "https://ipinfo.io/json";
            HttpHelper.getAsync(url[0], (success2, response2) -> {
                if (success2) {
                    try {
                        JSONObject data = new JSONObject(response2);
                        callback.onSuccess(data.getString("ip"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    return;
                }
                error(url[0] + " 失败，继续下一个");
                url[0] = "https://ifconfig.me/ip";
                HttpHelper.getAsync(url[0], (success3, response3) -> {
                    if (success3) {
                        callback.onSuccess(response3);
                        return;
                    }
                    error(url[0] + " 失败，继续下一个");
                    url[0] = "https://icanhazip.com";
                    HttpHelper.getAsync(url[0], (success4, response4) -> {
                        if (success4) {
                            callback.onSuccess(response4);
                            return;
                        }
                        error(url[0] + " 失败，继续下一个");
                        url[0] = "https://httpbin.org/ip";
                        HttpHelper.getAsync(url[0], (success5, response5) -> {
                            if (success5) {
                                try {
                                    JSONObject data = new JSONObject(response5);
                                    callback.onSuccess(data.getString("origin"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                return;
                            }
                            error(url[0] + " 失败，继续下一个");
                            url[0] = "https://api.myip.com";
                            HttpHelper.getAsync(url[0], (success6, response6) -> {
                                if (success6) {
                                    try {
                                        JSONObject data = new JSONObject(response6);
                                        callback.onSuccess(data.getString("ip"));
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        });
                    });
                });
            });
        });
    }

    public static void getCountryCodeByIP(String ip, StringCallback callback) {
        final String[] url = {"https://api.ipdata.co/" + ip + "?api-key=45be73fa957e328ec5e9084d56fc3e198c21e4ae1954290b28fd4a56"};
        HttpHelper.getAsync(url[0], (success, response) -> {
            if (success) {
                try {
                    JSONObject data = new JSONObject(response);
                    callback.onSuccess(data.getString("country_code"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;
            }
            error(url[0] + " 失败，继续下一个");
            url[0] = "https://ipinfo.io/" + ip + "/json";
            HttpHelper.getAsync(url[0], (success2, response2) -> {
                if (success2) {
                    try {
                        JSONObject data = new JSONObject(response2);
                        callback.onSuccess(data.getString("country"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        });
    }

    private static void log(String msg) {
        Log.i("[B]" + IPAndCountry.class.getSimpleName(), msg);
    }

    private static void error(String msg) {
        Log.e("[B]" + IPAndCountry.class.getSimpleName(), msg);
    }
}
